﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using ERP.Manager.DataEntity;
using ERP.Manager.BusinessServices;
using ERP.Manager.Controllers.CustomAttributes;
namespace ERP.Manager.Controllers
{
    [CheckLoginAttribute]
    public class MessageController : Controller
    {
        private readonly MessageinfoService messageservice;
        private readonly ContextService contextservice;
        private readonly MscallerinfoService mscallerinfoservice;
        public MessageController() { }
        public MessageController(MessageinfoService messageservice, ContextService contextservice, MscallerinfoService mscallerinfoservice)
        {
            this.messageservice = messageservice;
            this.contextservice = contextservice;
            this.mscallerinfoservice = mscallerinfoservice;
        }

        #region 显示公告信息页面
        public ActionResult Showmessage(int? page)
        {
            int pagecount = page ?? 1;
            List<Messageinfo> list = messageservice.getAllMessages(pagecount);
            ViewData["message"] = list;
            int messgecount = messageservice.getAllMessageCount();
            ViewData["messgecount"] = messgecount;
            return View();
        }
        #endregion

        #region 增加公告信息页面
        [CheckPersonnel]
        public ActionResult Addmessage()
        {
            return View();
        }
        #endregion

        #region 修改公告信息页面
        [CheckPersonnel]
        public ActionResult MessageUpdate(string messageid)
        {
            Messageinfo me = new Messageinfo();
            me.messageId = int.Parse(messageid);
            Messageinfo message = messageservice.AllMessageDetail(me);
            ViewData["message"] = message;
            return View();
        }
        #endregion

        #region 公告信息详细页面
        public ActionResult MessageDetail(string messageid)
        {
            Messageinfo me = new Messageinfo();
            me.messageId = int.Parse(messageid);
            Messageinfo message = messageservice.AllMessageDetail(me);
            ViewData["message"] = message;
            int mscallerinfocount = mscallerinfoservice.allgetmscallerinfocount(int.Parse(messageid));
            ViewData["mscallerinfocount"] = mscallerinfocount;
            return View();
        }
        #endregion

        #region Ajax添加公告信息
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public string AjaxAddmessage(string messagetitle, string messagecontent)
        {
            try
            {
                Messageinfo message = new Messageinfo();
                message.messageTitle = messagetitle;
                message.messageContent = messagecontent;
                message.UserInfo.userId = contextservice.UserId;
                message.publishDate = DateTime.Now.AddDays(0);
                int rowcount = messageservice.AddMessage(message);
                if (rowcount > 0)
                {
                    System.Threading.Thread.Sleep(1000);
                    return "success";
                }
                else
                {
                    System.Threading.Thread.Sleep(1000);
                    return "error";
                }
            }
            catch (Exception ex)
            {
                System.Threading.Thread.Sleep(1000);
                return "" + ex.Message;
            }
        }
        #endregion

        #region Ajax删除公告信息
        [CheckPersonnel]
        [AcceptVerbs(HttpVerbs.Post)]
        public string messageDelete(string messageid)
        {
            try
            {
                int messagesid = int.Parse(messageid.ToString());
                int rowcount = messageservice.DeleteMessage(messagesid);
                if (rowcount > 0)
                {
                    return "success";
                }
                else
                {
                    return "error";
                }
            }
            catch (Exception ex)
            {
                return ""+ ex.Message;
            }
        }
        #endregion

        #region Ajax修改公告信息
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public string MessageUpdate(string messageid, string messagetitle, string messagecontent)
        {
            try
            {
                Messageinfo message = new Messageinfo();
                message.messageId = int.Parse(messageid);
                message.messageTitle = messagetitle;
                message.messageContent = messagecontent;
                int rowcount = messageservice.UpdateMessage(message);
                if (rowcount > 0)
                {
                    System.Threading.Thread.Sleep(1000);
                    return "success";
                }
                else
                {
                    System.Threading.Thread.Sleep(1000);
                    return "error";
                }
            }
            catch (Exception ex)
            {
                System.Threading.Thread.Sleep(1000);
                return "" + ex.Message;
            }
        }
        #endregion

        #region  Ajax创建阅读公告信息的用户
        public string AjaxCreateMyMenuallgetmscallerinfo(string index, string messageid)
        {
            int pagecount = int.Parse(index);
            int messageids = int.Parse(messageid.ToString());
            List<Mscallerinfo> list = mscallerinfoservice.allgetmscallerinfo(pagecount, messageids);
            var html = "";
            if (list.Count > 0)
            {
                foreach (var item in list)
                {
                    html += "<div><table width='100%' style='height:100%;' border='0px' cellpadding='0' cellspacing='0'><tr><td align='center' style='width:50%;'>" + item.userinfo.userName + "</td><td align='center' style='width:50%;'>" + item.callertime.ToString("yyyy-MM-dd") + "</td></tr></table></div>";
                }
            }
            else
            {
                html += "<table width='100%' style='height:100%;' align='center'><tr><td align='center'>本公告无人查阅</td></tr></table>";
            }

            return html;
        }
        #endregion

        #region Ajax创建阅读公告信息的用户的所有人数的总数
        public string AjaxGetallgetmscallerinfocount(string index, string messageid)
        {
            int pagecount = int.Parse(index);
            int messageids = int.Parse(messageid);
            List<Mscallerinfo> list = mscallerinfoservice.allgetmscallerinfo(pagecount, messageids);
            return list.Count.ToString();
        }
        #endregion

        #region Ajax判断当前用户是否已经查阅公告信息的方法
        public string Ajaxestimatemscallerinfo(string messageid)
        {
            string userid = contextservice.UserId;
            List<Mscallerinfo> list = mscallerinfoservice.allgetmscallerinfomessageid(int.Parse(messageid), userid);
            if (list.Count > 0)
            {
                return "error";
            }
            else
            {
                return "success";
            }
        }
        #endregion

        #region Ajax添加当前用户查阅公告信息的方法
        public string Ajaxaddmscalerinfo(string messageid)
        {
            try
            {
                Mscallerinfo mscallerinfo = new Mscallerinfo();
                mscallerinfo.messageinfo.messageId = int.Parse(messageid);
                mscallerinfo.userinfo.userId = contextservice.UserId;
                mscallerinfo.callertime = DateTime.Now.AddDays(0);
                int rowcount = mscallerinfoservice.addmscallerinfo(mscallerinfo);
                if (rowcount > 0)
                {
                    return "success";
                }
                else
                {
                    return "error";
                }
            }
            catch (Exception ex)
            {
                return "" + ex;
            }
        }
        #endregion
    }
}
